package com.construction.ccb

import android.app.Application
import com.construction.base_lib.dialog.BubbleDialog
import com.construction.base_lib.net.converter.SerializationConverter
import com.construction.base_lib.net.interceptor.GlobalHeaderInterceptor
import com.construction.base_lib.net.interceptor.RefreshTokenInterceptor
import com.construction.ccb.constants.Api
import com.drake.net.NetConfig
import com.drake.net.cookie.PersistentCookieJar
import com.drake.net.interceptor.LogRecordInterceptor
import com.drake.net.okhttp.setConverter
import com.drake.net.okhttp.setDebug
import com.drake.net.okhttp.setDialogFactory
import com.drake.net.okhttp.setRequestInterceptor
import okhttp3.Cache
import java.util.concurrent.TimeUnit

/**
 * @author: FiDo
 * @date: 2024/3/11
 * @des:
 */
class BaseApp:Application() {


    override fun onCreate() {
        super.onCreate()

        initNet()
    }

    private fun initNet() {
        NetConfig.initialize(Api.HOST, this) {

            // 超时设置
            connectTimeout(30, TimeUnit.SECONDS)
            readTimeout(30, TimeUnit.SECONDS)
            writeTimeout(30, TimeUnit.SECONDS)

            // 本框架支持Http缓存协议和强制缓存模式
            cache(Cache(cacheDir, 1024 * 1024 * 128)) // 缓存设置, 当超过maxSize最大值会根据最近最少使用算法清除缓存来限制缓存大小

            // LogCat是否输出异常日志, 异常日志可以快速定位网络请求错误
            setDebug(BuildConfig.DEBUG)

            // 添加持久化Cookie管理
            cookieJar(PersistentCookieJar(this@BaseApp))

            // AndroidStudio OkHttp Profiler 插件输出网络日志
            addInterceptor(LogRecordInterceptor(BuildConfig.DEBUG))

            addInterceptor(RefreshTokenInterceptor())
            // 添加请求拦截器, 可配置全局/动态参数
            setRequestInterceptor(GlobalHeaderInterceptor())

            // 数据转换器
            setConverter(SerializationConverter())

            // 自定义全局加载对话框
            setDialogFactory {
                BubbleDialog(it, "加载中....")
            }
        }
    }

}